# 基础版, 提供基础的对话能力
import requests
import json

class YuanBaoJiChuBan:
    def __init__(self, yuanbao_id = "naQivTmsDa", hy_user = "c903525ab38242efa670fc443640557b", hy_token = "amlqbRV7Feb+gXpvj/ixQsAX1rY/82hWXh1L0v4fBL7bIxLhKVeqRJfmAGL5248MUfWrls3/ZQj8NJyeCPA43Q==", GenerateImages_id="gtcnTp5C1G"):
        # 唯一ID
        self.yuanbao_id = yuanbao_id
        # 生成图片ID
        self.GenerateImages_id = GenerateImages_id
        # 用户id
        self.hy_user = hy_user
        # 密钥
        self.hy_token = hy_token
        # 基础url
        self.domain = "https://yuanbao.tencent.com"
        # 基础headers
        self.headers = {
            'accept': 'application/json, text/plain, */*',
            'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
            'origin': self.domain,
            'priority': 'u=1, i',
            'referer': f'{self.domain}/chat/{self.yuanbao_id}',
            'sec-fetch-dest': 'empty',
            'sec-fetch-mode': 'cors',
            'sec-fetch-site': 'same-origin',
            't-userid': self.hy_user,
            'user-agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1 Edg/135.0.0.0',
            'x-agentid': self.yuanbao_id,
            'x-id': self.hy_user,
            'x-instance-id': '5',
            'x-language': 'zh-CN',
            'x-os_version': 'iOS(13.2.3)-WebKit',
            'x-platform': 'ios',
            'x-requested-with': 'XMLHttpRequest',
            'x-source': 'web',
        }
        # 基础cookie
        self.cookies = {
            'hy_source': 'web',
            'hy_user': self.hy_user,
            'hy_token': self.hy_token,
        }


    

    # 创建对话
    def create_chat(self):
        # 接口地址
        chat_api = self.domain + "/api/user/agent/conversation/create"
        # 请求参数
        json_data = {
            'agentId': self.yuanbao_id,
        }

        response = requests.post(chat_api,  headers=self.headers, json=json_data, cookies=self.cookies).json()
        return response

    # 发送消息
    def send_message(self, message, chat_id, yuanbao_model="hunyuan_gpt_175B_0404", multimedia=[]):
        """
        yuanbao_model模型说明:
        - hunyuan_gpt_175B_0404: 腾讯元宝的基础模型，提供基础的对话能力。
        - hunyuan_t1: 腾讯元宝的T1模型，提供深度思考的回答。
        - deep_seek_v3: 腾讯元宝接入的deep_seel模型V3版本。
        - deep_seek: 腾讯元宝接入的deep_seel模型R1版本,拥有深度思考功能。

        multimedia参数说明:
        {
            "type": "txt",  # 类型,文本是txt,图片是image
            "docType": 和type一样
            "url": 文件url,
            "fileName": 文件名称,
            "size":111 文件大小,
            "width": 0图片专属,
            "height": 0图片专属
        }
        """
        # 接口地址
        send_api = self.domain + f"/api/chat/{chat_id}"
        # 请求参数
        data = {
        "model":"gpt_175B_0404",
        "prompt":message,
        "plugin":"Adaptive",
        "displayPrompt":message,
        "displayPromptType":1,
        "options":{
            "imageIntention":{
                "needIntentionModel":True,
                "backendUpdateFlag":2,
                "intentionStatus":True
                }
            },
            "multimedia":multimedia,
            "agentId":self.yuanbao_id,
            "supportHint":1,
            "version":"v2",
            "chatModelId":yuanbao_model
        }
        data = json.dumps(data)
        response = requests.post(send_api, cookies=self.cookies, headers=self.headers, data=data, stream=True)
        return response
        # 处理响应
        # if response.status_code == 200:
        #     self.handle_stream_response(response)
        # else:
        #     print("请求失败，状态码:", response.status_code)
    
    # 获取历史对话
    def get_chat_history(self, offset=0, limit=40):
        """
        参数说明:
        - offset: 偏移量，用于分页查询，默认为0,例如limit是1，那么此时offset就是0，如果想查询第二页，那么offset就是2。
        - limit: 每页的记录数，默认为1。
        """
        # 接口地址
        history_api = self.domain + '/api/user/agent/conversation/list'
        json_data = {
            'agentId': self.yuanbao_id,
            'offset': offset,
            'limit': limit,
            'filterGoodQuestion': True,
        }
        response = requests.post(history_api, cookies=self.cookies, headers=self.headers, json=json_data).json()
        return response

    # 获取对话内容
    def get_chat_content(self, chat_id, offset=0, limit=60):
        # 接口地址
        json_data = {
            'conversationId': chat_id,
            'offset': offset,
            'limit': limit,
        }
        response = requests.post(f'{self.domain}/api/user/agent/conversation/v1/detail', cookies=self.cookies, headers=self.headers, json=json_data).json()
        return response

    # 删除对话
    def delete_chat(self, chat_id):
        # 接口地址
        json_data = {
            'conversationIds': [
                chat_id,
            ],
            'uiOptions': {},
        }

        response = requests.post(f'{self.domain}/api/user/agent/conversation/v1/clear', cookies=self.cookies, headers=self.headers, json=json_data)
        return response.text

    # 处理流式响应
    def handle_stream_response(self, response):
        for line in response.iter_lines():
                if line:
                    try:
                        text = line.decode('utf-8')

                        if text.startswith('data: '):
                            text = text.replace('data: ', '')
                            # print("原始数据: " + text)
                            if text == "[DONE]":
                                print("对话结束")
                                yield 'data: {"type": "end", "msg": "对话结束"}\n\n'
                                break
                            text = json.loads(text)
                            if text['type'] == "text":
                                # 回复
                                if text.get("msg") is not None:
                                    print("普通回复: " + text['msg'])
                                else:
                                    continue
                            elif text['type'] == "think":
                                # 思考中
                                print("思考回复: " + text['content'])
                            elif text['type'] == "progress":
                                print("进度: " + str(text['value']))
                            elif text['type'] == "image":
                                print("图片: " + text['imageUrlLow'])
                            elif text['type'] == "style":
                                print("风格地址: " + text['thumbnailUrl'].replace(r'\u0026', '&'))
                            elif text['type'] == "error":
                                print("错误: " + text['msg'])
                            elif text['type'] == "removewatermark":
                                print("去除水印成品图: " + text['imageUrl'])
                            elif text['type'] == "clarity":
                                print("变清晰成品图: " + text['imageUrl'])
                            else:
                                continue
                            yield f'data: {json.dumps(text, ensure_ascii=False)}\n\n'
                               

                    except json.JSONDecodeError as e:
                        pass
                    except KeyError as e:
                        pass


if __name__ == '__main__':
    # 实例化
    yuanbao = YuanBaoJiChuBan()
    # 创建对话
    print(yuanbao.delete_chat("77bf4a0b-5b7e-4d2a-a8c5-9859bf86a8ca"))

